﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.Sql;

namespace MyShop
{
    public partial class _Default : Page
    {
        public List<string> ProductsList;
        public class ProductObj
        {
            public string ID { get; set; }
            public string GroupID { get; set; }
            public string CompanyID { get; set; }
            public string ModelName { get; set; }
            public string Description { get; set; }
            public string Price { get; set; }
            public string Count { get; set; }
            public string ImageFileUrl { get; set; }
        }
        public class ProductCompanyObj
        {
            public string CompanyID { get; set; }
            public string Name { get; set; }
        }

        public List<ProductObj> DataProduct { get; set; }
        public List<ProductCompanyObj> DataProductCompany { get; set; } 
        
        protected void Page_Load(object sender, EventArgs e)
        {
            Uri tempWeb = Request.Url;
            string prodId = Request.QueryString["productId"];
            if(!String.IsNullOrEmpty(tempWeb.Query))
            {
                string pattern = @"\?";
                Regex rgx = new Regex(pattern);
                string input = tempWeb.Query;
                string[] result = rgx.Split(input, 0, 0);

                string pattern1 = @"\=";
                Regex rgx1 = new Regex(pattern1);
                string input1 = result[1];
                string[] result1 = rgx1.Split(input1, 0, 0);

                if (result1[0] == "productId")
                {
                    string proc = "select Product.ID, Product.GroupID, Product.CompanyID, Product.ModelName, Product.Description, Product.Price, Product.Count, Images.ImageFileUrl from Product inner join Images on Product.ID = Images.ProductID where Product.ID =" + result1[1];
                    GetProductObj(proc , true);
                }
                if (result1[0] == "GroupProductId")
                {
                    string proc = "select Product.ID, Product.GroupID, Product.CompanyID, Product.ModelName, Product.Description, Product.Price, Product.Count, Images.ImageFileUrl from Product inner join Images on Product.ID = Images.ProductID where Product.GroupID =" + result1[1];
                    GetProductObj(proc, false);
                    
                    //List<string> prodList = new List<string>();
                    Session["SessionGroupProductId"] = result1[1];

                    using (SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString))
                    {
                        SqlCommand com = new SqlCommand("select ProductCompany.ID, ProductCompany.Name from ProductCompany inner join Product on ProductCompany.ID = Product.CompanyID where Product.GroupID =" + result1[1] + " GROUP BY ProductCompany.ID, ProductCompany.Name", sqlcon);
                        DataSet dataSet = new DataSet();
                        DataTable dataTable = new DataTable();
                        SqlDataAdapter dataAdapter = new SqlDataAdapter();
                        dataAdapter.SelectCommand = com;
                        dataAdapter.Fill(dataTable);
                        dataSet.Tables.Add(dataTable);
                        GetDataProductCompany(dataTable);
                    }

                }
                //else 
                //{
                //    using (SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString))
                //    {
                //        SqlCommand com = new SqlCommand("select * from ProductCompany", sqlcon);
                //        DataSet dataSet = new DataSet();
                //        DataTable dataTable = new DataTable();
                //        SqlDataAdapter dataAdapter = new SqlDataAdapter();
                //        dataAdapter.SelectCommand = com;
                //        dataAdapter.Fill(dataTable);
                //        dataSet.Tables.Add(dataTable);
                //        GetDataProductCompany(dataTable);
                //    }
                //}
                if (result1[0] == "companyId")
                {
                    string Group = "";
                    Group = (string)Session["SessionGroupProductId"];
                    if (Group != "" && Session["SessionGroupProductId"] != null)
                    {
                        string proc = "select Product.ID, Product.GroupID, Product.CompanyID, Product.ModelName, Product.Description, Product.Price, Product.Count, Images.ImageFileUrl from Product inner join Images on Product.ID = Images.ProductID where Product.CompanyID =" + result1[1] + " and Product.GroupID =" + Group;
                        GetProductObj(proc, false);
                        
                        using (SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString))
                        {
                            SqlCommand com = new SqlCommand("select * from ProductCompany where ProductCompany.ID = " + result1[1], sqlcon);
                            DataSet dataSet = new DataSet();
                            DataTable dataTable = new DataTable();
                            SqlDataAdapter dataAdapter = new SqlDataAdapter();
                            dataAdapter.SelectCommand = com;
                            dataAdapter.Fill(dataTable);
                            dataSet.Tables.Add(dataTable);
                            GetDataProductCompany(dataTable);
                        }
                    }
                    else {

                        string proc = "select Product.ID, Product.GroupID, Product.CompanyID, Product.ModelName, Product.Description, Product.Price, Product.Count, Images.ImageFileUrl from Product inner join Images on Product.ID = Images.ProductID where Product.CompanyID =" + result1[1];
                        GetProductObj(proc, false);
                       
                    }
                    
                }
                
            }
           else
           {
               string proc = "select Product.ID, Product.GroupID, Product.CompanyID, Product.ModelName, Product.Description, Product.Price, Product.Count, Images.ImageFileUrl from Product inner join Images on Product.ID = Images.ProductID";
               GetProductObj(proc, false);
              
               using (SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString))
               {
                   SqlCommand com = new SqlCommand("select * from ProductCompany", sqlcon);
                   DataSet dataSet = new DataSet();
                   DataTable dataTable = new DataTable();
                   SqlDataAdapter dataAdapter = new SqlDataAdapter();
                   dataAdapter.SelectCommand = com;
                   dataAdapter.Fill(dataTable);
                   dataSet.Tables.Add(dataTable);
                   GetDataProductCompany(dataTable);
               }
           }
            

           List<string> listProd = new List<string>();
           listProd = (List<string>)Session["ProductsInBasket"];
           SiteMaster m = (SiteMaster)Page.Master;
          
           if (listProd != null)
           {
               m.CountProd = listProd.Count;
           }
           else
           {
               m.CountProd = 0;
           }

            if (!IsPostBack)
            {   
            }
        }

        public void GetProductObj(string proc, bool isFullInfo) {
            using (SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString))
            {
                SqlCommand com = new SqlCommand(proc, sqlcon);
                DataSet dataSet = new DataSet();
                DataTable dataTable = new DataTable();
                SqlDataAdapter dataAdapter = new SqlDataAdapter();
                dataAdapter.SelectCommand = com;
                dataAdapter.Fill(dataTable);
                dataSet.Tables.Add(dataTable);
                GetDataProduct(dataTable, isFullInfo);

            }
        }

        public void GetDataProduct(DataTable prod, bool isFullInformation)
        {
                List<ProductObj> mainResult = new List<ProductObj>();
            if (prod.Rows.Count != 0 )
            {
                
                foreach (DataRow item in prod.Rows)
                {
                    mainResult.Add(new ProductObj
                    {
                        ID = item["ID"].ToString(),
                        GroupID = item["GroupID"].ToString(),
                        CompanyID = item["CompanyID"].ToString(),
                        ModelName = item["ModelName"].ToString(),
                        Description = item["Description"].ToString(),
                        Price = item["Price"].ToString(),
                        Count = item["Count"].ToString(),
                        ImageFileUrl = item["ImageFileUrl"].ToString()
                    });
                }
                DataProduct = mainResult;
                BindProduct( isFullInformation);
            }

            
        }
        public void GetDataProductCompany(DataTable prod)
        {
            List<ProductCompanyObj> mainResult = new List<ProductCompanyObj>();
            if (prod.Rows.Count != 0)
            {
                foreach (DataRow item in prod.Rows)
                {
                    mainResult.Add(new ProductCompanyObj
                    {
                        CompanyID = item["ID"].ToString(),
                        Name = item["Name"].ToString()
                    });
                }
                DataProductCompany = mainResult;
                BindProductCompany();
            }
        }
        public void BindProduct( bool isFullInfo)
        {

            if (isFullInfo)
            {
                DetailRepeaterProduct.DataSource = DataProduct;
                DetailRepeaterProduct.DataBind();
            }
            else
            {
                RepeaterProduct.DataSource = DataProduct;
                RepeaterProduct.DataBind();
            }
        }
        public void BindProductCompany()
        {
            RepeaterCompany.DataSource = DataProductCompany;
            RepeaterCompany.DataBind();
        }

        public void AddProductInBasket(Object sender, EventArgs e)
        {
            List<string> prodList = new List<string>();
            Button button = sender as Button;
            string idProd = button.CommandName;
            if (Session["ProductsInBasket"] != null)
            {
                var tempExist = false;
                ProductsList = (List<string>)Session["ProductsInBasket"];
                foreach (string str in ProductsList)
                {
                    if (idProd == str)
                    {
                        tempExist = true;
                    }
                }
                if (!tempExist)
                {
                    ProductsList.Add(idProd);
                }
            }
            else
            {
                prodList.Add(idProd);
                ProductsList = prodList;
            }
            SiteMaster m = (SiteMaster)Page.Master;
            m.CountProd = ProductsList.Count;
            Session["ProductsInBasket"] = ProductsList;
        }
        public void AllInfoProduct(Object sender, EventArgs e)
        {
            var idx = 1;
            LinkButton linkButton = sender as LinkButton;
            Response.Redirect("Default.aspx?Data=" + Server.UrlEncode(linkButton.ID));
        }
    }
}